﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    /// <summary>
    /// 积分管理
    /// </summary>
    public class IntegralManageController : BaseController
    {
        // 操作员
        protected Model.View_OperUser operUser;

        // 接收表单提交 string SetOrder_Int (int OperID,string json, int tag, string YZM, int RETID)
        /// <summary>
        /// 保存平台积分
        /// </summary>
        /// <param name="model">积分管理参数</param>
        /// <returns></returns>
        public ActionResult Submit(Model.Order_Int_T model)
        {
            try
            {
                if (model != null)
                {
                    // 获取操作员
                    Model.View_OperUser operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;
                    string value = ObjectToJson(model);
                    string result = "";
                    if (model.IntID != "" && model.IntID != null)//如果用户ID为空则是新增，如果不为空则为修改
                    {
                        result = SRS.SetOrder_Int(operUser.OperID, ObjectToJson(model), 2, YZM, 1);
                    }
                    else
                    {
                        result = SRS.SetOrder_Int(operUser.OperID, ObjectToJson(model), 1, YZM, 1);
                    }
                    JObject jb = JObject.Parse(result);
                    if (jb["BS"].ToString() == "1")
                        return Content(jb["BS"].ToString());
                    else
                        return Content(jb["Msg"].ToString());
                }
            }
            //return Content("数据异常，请联系管理员");
            catch (Exception)
            {
                //return null;
                return RedirectToAction("LoginPage", "Login");  //czx，修改为登录者的Session异常时，直接跳转到登录页面

            }
            return Content("数据异常，请联系管理员");
        }
        // 主页
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetData() 
        {
            operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;
            Model.View_Order_Int M = new Model.View_Order_Int();//获取平台积分
            if (operUser != null)
            {
                ViewBag.UserPowers = operUser.RolePower;
            }
            else
            {
                return RedirectToAction("LoginPage", "Login");  //czx，修改为登录者的Session异常时，直接跳转到登录页面
            }

            string result = SRS.GetOrder_Int("0", YZM, 1);
            if (!string.IsNullOrEmpty(result) && result.IndexOf("\"BS\":\"1\"") > -1)
            {
                JObject search = JObject.Parse(result);//将服务返回的结果进行优化
                IList<JToken> jieguoMer = search["rows"].Children().ToList();//解析返回的结果
                if (jieguoMer.Count == 1)
                {
                    foreach (JToken item in jieguoMer)
                    {
                        M.MerID = item["MerID"].ToString();
                        M.MerName = item["MerName"].ToString();
                        M.IntID = item["IntID"].ToString();
                        M.IntChe = int.Parse(item["IntChe"].ToString());
                        M.IntUn = int.Parse(item["IntUn"].ToString());
                        M.IntY = int.Parse(item["IntY"].ToString());
                        M.IsOpen = int.Parse(item["IsOpen"].ToString());
                        break;
                    }
                }
            }
            return Json(M);
        }
    }
}
